import { serviceList } from "@/api/product";
import { solutionList } from "@/api/scheme";
import { tqProducttypeTree } from "@/api/ecology";
import { publicSrc } from "@/utils/common";

export default createStore({
  state: {
    hireShow: false, // 招聘弹窗
    serviceData: [], // 服务产品列表
    solutionData: [] // 解决方案列表
  },
  mutations: {
    SET_HIRE(state: any, value: any) {
      state.hireShow = value
    },
    SET_SERVICE(state: any, value: any) {
      state.serviceData = value
    },
    SET_SOLUTION(state: any, value: any) {
      state.solutionData = value
    },
    SET_ECOLOGY(state: any, value: any) {
      state.ecologyData = value
    }
  },
  actions: {
    // 获取服务产品列表
    setService({ commit }: any) {
      return new Promise((resolve, reject) => {
        let data = {
          pageNum: 1, // 当前页
          pageSize: 8, // 每页条数
          status: 0, // 0显示 1隐藏
        }
        let list = <any>[];
        serviceList(data).then((res: any) => {
          if (res.code === 200) {
            list = res.rows.map((item: any) => {
              return {
                name: item.name,
                path: "/pc/product",
                query: {
                  id: item.id,
                },
                desc: item.introduce,
                icon: publicSrc(item.image),
                hot: item.top,
              };
            });
            commit('SET_SERVICE', list)
            resolve(list)
          } else {
            reject(res.msg)
          }
        }).catch((err: any) => {
          reject(err)
        })
      })
    },
    // 获取生态产品列表
    setEcology({ commit }: any) {
      return new Promise((resolve, reject) => {
        let data = {
          pageNum: 1, // 当前页
          pageSize: 8, // 每页条数
          status: 0, // 0显示 1隐藏
        }
        let list = <any>[];
        tqProducttypeTree(data).then((res: any) => {
          if (res.code === 200) {
            list = res.data.map((item: any) => {
              return {
                label: item.name,
                value: item.id,
                children: item.children ? item.children.map((child: any) => {
                  return {
                    label: child.name,
                    value: child.id,
                  }
                }) : []
              };
            });
            commit('SET_ECOLOGY', list)
            resolve(list)
          } else {
            reject(res.msg)
          }
        }).catch((err: any) => {
          reject(err)
        })
      })
    },
    // 获取解决方案列表
    setSolution({ commit }: any) {
      return new Promise((resolve, reject) => {
        let data = {
          pageNum: 1, // 当前页
          pageSize: 8, // 每页条数
          status: 0, // 0显示 1隐藏
        }
        let list = <any>[];
        solutionList(data).then((res: any) => {
          if (res.code === 200) {
            list = res.rows.map((item: any) => {
              return {
                name: item.name,
                path: "/pc/scheme",
                query: {
                  id: item.id,
                },
                desc: item.synopsis,
                icon: publicSrc(item.image),
                hot: item.top,
              };
            });
            commit('SET_SOLUTION', list)
            resolve(list)
          } else {
            reject(res.msg)
          }
        }).catch((err: any) => {
          reject(err)
        })
      })
    }
  },
  getters: {
    hireShow: (state: any) => state.hireShow,
    serviceData: (state: any) => state.serviceData,
    solutionData: (state: any) => state.solutionData
  },
  modules: {
  }
})